package agents.interaction;

import game.constants.ReferenceAgent;
import game.constants.ReferenceObject;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.HumanInteraction;

import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionGatherVespeneGas;
import actions.ActionGiveVespeneGas;
import actions.ActionPickUpObject;
import actions.ActionReciveVespeneGas;
import actions.ActionXYtoMove;
import extra.ObjectId;
import extra.Position;

public class InteractionGatherVespeneGas extends HumanInteraction{
	
	ObjectId mineralId;
	

	ActionGiveVespeneGas actionGiveVespeneGas;
	ActionXYtoMove actionXYtoMove;
	ActionGatherVespeneGas actionGatherVespeneGas;
	
	SpetialFeature timeToGather;
	SpetialFeature vespeneGasQuantityToGather;
	SpetialFeature vespeneGasQuantity;
	SpetialFeature attackRange;
	SpetialFeature xToMove;
	SpetialFeature yToMove;

	WorldObject commandCenterToCarryMinerals=null;
	
	double contTime=0;
	
	
	@Override
	public void initializeParametersList() {
		
		mineralId = new ObjectId();
		addParameter(mineralId);
		
	}

	@Override
	public void initializeActionList() {
		
		actionXYtoMove= new ActionXYtoMove();
		addAction(actionXYtoMove);

		actionGiveVespeneGas= new ActionGiveVespeneGas();
		addAction(actionGiveVespeneGas);
		
		actionGatherVespeneGas = new ActionGatherVespeneGas();
		addAction(actionGatherVespeneGas);
		
	}

	@Override
	public void initializeSpecialFeatures() {
		
		timeToGather = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TIME_TO_GATHER_VESPENE_GAS);
		addSpetialFeature(timeToGather);
		
		
		vespeneGasQuantityToGather= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VESPENE_GAS_QUANTITY_TO_GATHER);
		addSpetialFeature(vespeneGasQuantityToGather);
		
		vespeneGasQuantity= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VESPENE_GAS_QUANTITY);
		addSpetialFeature(vespeneGasQuantity);
		
		attackRange = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_ATTACK_RANGE);
		addSpetialFeature(attackRange);
		
		xToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_X_TO_MOVE);
		addSpetialFeature(xToMove);
		
		yToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_Y_TO_MOVE);
		addSpetialFeature(yToMove);
	}

	@Override
	public void initializeName() {
		name= "Gather Vespene Gas";
		
	}

	@Override
	public void initializeId() {
		this.id= ReferenceAgent.AGENT_HUMAN_INTERACTION_GATHER_VESPENE_GAS;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		if(!object.isActive()){
			return;
		}
		object.desactivateAllHumanInteractions();
		isActive=true;
		
		if(vespeneGasQuantity.getValue()<vespeneGasQuantityToGather.getValue()){
			gatherVespeneGas(object,universe,time);
			return;
		}
		
		returnVespeneGasMinerals(object,universe,time);
		
		
	}

	private void returnVespeneGasMinerals(WorldObject object, Universe universe,double time) {
		
		//System.out.println("tengo los minerales para llevarlos al centro de comando");
		ArrayList<WorldObject> listCommandCenter = universe.getListObjects(ReferenceObject.OBJECT_REFERENCE_COMMAND_CENTER1);
		
		
		double distance=Integer.MAX_VALUE;
		
		for(WorldObject obj : listCommandCenter){
			if(obj.getOwner()==object.getOwner()){
				double dAux = Position.DistanceBetween(obj.getPosition(), object.getPosition());
				if(dAux<distance){
					distance=dAux;
					commandCenterToCarryMinerals=obj;
				}
			}
		}

		if(commandCenterToCarryMinerals==null){
			return;
		}

		double x =commandCenterToCarryMinerals.getImageObject().getImageToShow().getWidth()*(0.66)+commandCenterToCarryMinerals.getPosition().getX();
		double y =commandCenterToCarryMinerals.getPosition().getY();
		distance = (Position.DistanceBetween(object.getPosition(), new Position(x,y)));
		
		if(distance>attackRange.getValue()){
			
			if(xToMove.getValue()==x && yToMove.getValue()==y){

				return;
			}


			actionXYtoMove.setSource(object);
			actionXYtoMove.setTarget(object);
			actionXYtoMove.setPosition(new Position(x,y));
			universe.setAction(actionXYtoMove);
			return;
		}
		
		actionGiveVespeneGas.setSource(object);
		actionGiveVespeneGas.setTarget(object);
		System.out.println("Tengo vespene gas:"+vespeneGasQuantity.getValue()+" ID:"+object.getID());
		actionGiveVespeneGas.setQuantity(vespeneGasQuantity.getValue());
		universe.setAction(actionGiveVespeneGas);
		
		ActionReciveVespeneGas actionReciveVespeneGas = new ActionReciveVespeneGas();
		actionReciveVespeneGas.setQuantity(vespeneGasQuantity.getValue());
		actionReciveVespeneGas.setSource(object);
		actionReciveVespeneGas.setTarget(commandCenterToCarryMinerals);
		universe.setAction(actionReciveVespeneGas);
		
		
	}

	private void gatherVespeneGas(WorldObject object,Universe universe, double time) {
		WorldObject refinery = universe.getObject(mineralId.getObjectId());
		if(refinery==null){
			return;
		}
		if(refinery.getObjectReference()!=ReferenceObject.OBJECT_REFERENCE_REFINERY1){
			System.out.println("NO soy refineria");
			return;
		}
		
		double x =refinery.getPosition().getX()+refinery.getImageObject().getImageToShow().getWidth()*(0.66);
		double y =refinery.getPosition().getY();
		
		double distance = (Position.DistanceBetween(object.getPosition(), new Position(x,y)));
		
		if(distance>attackRange.getValue()){
			if(xToMove.getValue()==x && yToMove.getValue()== y){
				return;
			}
			actionXYtoMove.setSource(object);
			actionXYtoMove.setTarget(object);
			actionXYtoMove.setPosition(new Position(x,y));
			universe.setAction(actionXYtoMove);
			return;
		}
		
		
		System.out.println("BUSCO GAS");
		
		if(!object.isActive()){
			return;
		}
		
		ActionPickUpObject actionPickUpObject = new ActionPickUpObject();
		actionPickUpObject.setSource(object);
		actionPickUpObject.setTarget(refinery);
		actionPickUpObject.setObjetToPickUp(object);
		universe.setAction(actionPickUpObject);
		
		actionGatherVespeneGas= new ActionGatherVespeneGas();
		actionGatherVespeneGas.setSource(object);
		actionGatherVespeneGas.setTarget(object);
		universe.setAction(actionGatherVespeneGas);
		
		
		
		
		
	}

}
